<?php 

/* Copyright 2010 John Hooley

	  
    This file is part of Portfolio Zen.


    Portfolio Zen is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.



    Portfolio Zen is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.



    You should have received a copy of the GNU General Public License

    along with Portfolio Zen.  If not, see <http://www.gnu.org/licenses/>.

 */ 



defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class PortfoliozenViewGalleries extends JView {

  public $base = null;
  public $params = false;
  public $option = null;

  public function display(){
    $this->_setViewVariables();
    $galleries =& $this->get('Galleries');
    $this->assignRef('galleries', $galleries);
    $this->assignRef('initial', $this->_getInitial($galleries));
    $this->_loadTemplate();
    parent::display();
  }

  private function _setViewVariables(){
    global $option;
    $this->option = $option;
    $this->base = JURI::base() . "components/{$this->option}/";
    $this->params = $this->_getParams();
  }

  private function _loadTemplate(){
    $layoutClass = $this->_getLayoutClass();
    if(!$layoutClass) throw new Exception('Layout does not exist');
    $layoutClass->build();
    $this->_loadResources($layoutClass);
  }

  private function _getLayoutClass(){
    $layout = JRequest::getCmd('layout', 'default');
    require_once(dirname(__FILE__) . DS . 'layouts' . DS . $layout . '.php');
    $class = 'GalleriesLayoutHelper' . ucfirst($layout);
    if(class_exists($class)) return new $class($this);
    return false;
  }

  private function _loadResources(GalleriesLayoutHelper &$layoutClass){
    $doc =& JFactory::getDocument();
    $resources = $layoutClass->getResources();
    foreach($resources as $resource){
      foreach($resource as $key => $value){
	$method = "add{$key}";
	$doc->$method($value);
      }
    }
  }

  private function _getParams(){
    $app =& JFactory::getApplication();    
    return $app->getParams($this->option);
  }

  private function _getInitial($galleries){
    $keys = array_keys($galleries);
    return $keys[0];
  }

}


interface GalleriesLayoutHelper {

  public function __construct(&$view);

  public function build();

  public function getResources();

}
